import { defineConfig, loadEnv } from "vite"
import path from "path"
import createVitePlugins from "./vite/plugins"

import postCssPxToRem from "postcss-pxtorem"
export default defineConfig(({ mode, command }) => {
  const env = loadEnv(mode, process.cwd())
  const { VITE_APP_ENV } = env
  return {
    // 部署生产环境和开发环境下的URL。
    // 默认情况下，vite 会假设你的应用是被部署在一个域名的根路径上
    base: VITE_APP_ENV === "production" ? "/" : "/",
    plugins: createVitePlugins(env, command === "build"),
    resolve: {
      alias: {
        // 设置路径
        "~": path.resolve(__dirname, "./"),
        // 设置别名
        "@": path.resolve(__dirname, "./src"),
        // 设置别名
        $: path.resolve(__dirname, "./src/api"),
      },
      extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".vue"],
    },
    // 代理 相关配置
    server: {
      port: 8989,
      host: true,
      open: true,

      proxy: {
        "/dev-api": {
          target: env.VITE_BASE_URL,
          // target: 'http://192.168.0.56:8080',
          changeOrigin: true,
          rewrite: (p) => p.replace(/^\/dev-api/, ""),
        },
        "/up": {
          target: "http://192.168.0.71:9300",
          changeOrigin: true,
          rewrite: (p) => p.replace(/^\/dev-api/, ""),
        },
      },
    },
    css: {
      preprocessorOptions: {
        scss: {},
      },
      postcss: {
        plugins: [
          {
            postcssPlugin: "internal:charset-removal",
            AtRule: {
              charset: (atRule) => {
                if (atRule.name === "charset") {
                  atRule.remove()
                }
              },
            },
          },
          postCssPxToRem({
            rootValue: env.VITE_APP_BASE_FONT_SIZE,
            propList: ["*"],
          }),
        ],
      },
    },
  }
})
